package com.wanxi.education.service.impl;

import com.github.pagehelper.PageHelper;
import com.wanxi.education.mapper.PermissionMapper;
import com.wanxi.education.model.pojo.Permission;
import com.wanxi.education.model.vo.user.PermissionVO;
import com.wanxi.education.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Set;

/**
 * @Author: Kang
 * @Date: 2021/9/27 9:35
 * @Description: 权限业务实现
 * @Version: 1.0
 */
@Service
public class PermissionServiceImpl implements PermissionService {

    @Autowired
    private PermissionMapper permissionMapper;


    @Override
    public Set<PermissionVO> findByRoleId(Set<Long> roldIds) {
        Set<PermissionVO> permissionVOSet = permissionMapper.findByRoleId(roldIds);
        return permissionVOSet;
    }

    /**
     * 分页查询权限
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public List<PermissionVO> findPage(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<PermissionVO> permissionVOList = permissionMapper.findPage();
        return permissionVOList;
    }

    /**
     * 新增权限信息
     * @param permissionVO
     * @return
     */
    @Override
    public Boolean add(PermissionVO permissionVO) {
        Permission permission = Permission.builder()
                .name(permissionVO.getName())
                .description(permissionVO.getDescription())
                .uri(permissionVO.getUri())
                .createTime(new Date())
                .updateTime(new Date()).build();
        int i = permissionMapper.add(permission);
        return i == 1 ?true:false;
    }

    /**
     * 通过权限Id查询所属权限基本信息
     * @param permissionId
     * @return
     */
    @Override
    public PermissionVO findByPermissionId(Long permissionId) {
        PermissionVO permissionVO = permissionMapper.findByPermissionId(permissionId);
        return permissionVO;
    }

    /**
     * 通过权限Id修改所属权限基本信息
     * @param permissionVO
     * @return
     */
    @Override
    public Boolean updateById(PermissionVO permissionVO) {
        Permission permission = Permission.builder()
                .id(permissionVO.getId())
                .name(permissionVO.getName())
                .description(permissionVO.getDescription())
                .uri(permissionVO.getUri())
                .updateTime(new Date()).build();
        int i = permissionMapper.updateById(permission);
        return i == 1 ?true:false;
    }

    /**
     * 通过权限Id删除所属权限
     * @param permissionId
     * @return
     */
    @Override
    public Boolean deleteByPermissionId(Long permissionId) {
        int i = permissionMapper.deleteByPermissionId(permissionId);
        return i == 1 ?true:false;
    }

    @Override
    public List<PermissionVO> findAll() {
        return permissionMapper.findAll();
    }

}
